from requests import ConnectionError

import plotly.plotly as py
import plotly.graph_objs as go


PLOTLY_USERNAME = 'jocicmarko'
PLOTLY_API_KEY = 'fqoqlumqe3'
py.sign_in(username=PLOTLY_USERNAME, api_key=PLOTLY_API_KEY)

plots_data = dict()


def build_chart(name):
    X = []
    y = []
    plots_data[name] = [X, y]
    trace0 = go.Scatter(
        x=X,
        y=y,
        mode='lines+markers',
        name=name
    )
    data = [trace0]

    # Plot and embed in ipython notebook!
    # py.iplot(data, filename='line-mode')
    try:
        plot_url = py.plot(data, filename=name, auto_open=False, overwrite=True)
        print(plot_url)
    except ConnectionError:
        print("Graph was not updated due to connection problem")


def build_loss_val_graph(name, multitask=False):
    X = []
    y = []
    if not multitask:
        plots_data[name] = [[], [], [], [], []]
        diastole_train_loss = go.Scatter(
            x=X,
            y=y,
            mode='lines+markers',
            name='diastole_train_loss'
        )
        systole_train_loss = go.Scatter(
            x=X,
            y=y,
            mode='lines+markers',
            name='systole_train_loss'
        )
        diastole_val_loss = go.Scatter(
            x=X,
            y=y,
            mode='lines+markers',
            name='diastole_val_loss'
        )
        systole_val_loss = go.Scatter(
            x=X,
            y=y,
            mode='lines+markers',
            name='systole_val_loss'
        )
        data = [diastole_train_loss, systole_train_loss, diastole_val_loss, systole_val_loss]
    else:
        plots_data[name] = [[], [], []]
        multitask_train_loss = go.Scatter(
            x=X,
            y=y,
            mode='lines+markers',
            name='multitask_train_loss'
        )
        multitask_val_loss = go.Scatter(
            x=X,
            y=y,
            mode='lines+markers',
            name='multitask_val_loss'
        )
        data = [multitask_train_loss, multitask_val_loss]
    # Plot and embed in ipython notebook!
    # py.iplot(data, filename='line-mode')
    try:
        plot_url = py.plot(data, filename=name, auto_open=False, overwrite=True)
        print(plot_url)
    except ConnectionError:
        print("Graph was not updated due to connection problem")


def build_crps_graph(name):

    plots_data[name] = [[], [], []]
    crps = go.Scatter(
        x=[],
        y=[],
        mode='lines+markers',
        name='crps'
    )
    val_crps = go.Scatter(
        x=[],
        y=[],
        mode='lines+markers',
        name='val_crps'
    )
    data=[crps, val_crps]

    # Plot and embed in ipython notebook!
    # py.iplot(data, filename='line-mode')
    try:
        plot_url = py.plot(data, filename=name, auto_open=False, overwrite=True)
        print(plot_url)
    except ConnectionError:
        print("Graph was not updated due to connection problem")


def update_loss_val_graph(name, X, y_diastole_train_loss, y_diastole_val_loss, y_systole_train_loss=None, y_systole_val_loss=None, multitask=False):
    if not multitask:
        data = plots_data[name]
        X_update, y_diastole_train_loss_update, y_systole_train_loss_update, y_diastole_val_loss_update, y_systole_val_loss_update = data
        X_update.append(X)
        y_diastole_train_loss_update.append(y_diastole_train_loss)
        y_systole_train_loss_update.append(y_systole_train_loss)
        y_diastole_val_loss_update.append(y_diastole_val_loss)
        y_systole_val_loss_update.append(y_systole_val_loss)
        plots_data[name] = [X_update, y_diastole_train_loss_update, y_systole_train_loss_update, y_diastole_val_loss_update, y_systole_val_loss_update]
        diastole_train_loss = go.Scatter(
            x=X_update,
            y=y_diastole_train_loss_update,
            mode='lines+markers',
            name='diastole_train_loss'
        )
        systole_train_loss = go.Scatter(
            x=X_update,
            y=y_systole_train_loss_update,
            mode='lines+markers',
            name='systole_train_loss'
        )
        diastole_val_loss = go.Scatter(
            x=X_update,
            y=y_diastole_val_loss_update,
            mode='lines+markers',
            name='diastole_val_loss'
        )
        systole_val_loss = go.Scatter(
            x=X_update,
            y=y_systole_val_loss_update,
            mode='lines+markers',
            name='systole_val_loss'
        )
        data = [diastole_train_loss, systole_train_loss, diastole_val_loss, systole_val_loss]
    else:
        data = plots_data[name]
        X_update, y_diastole_train_loss_update, y_diastole_val_loss_update = data
        X_update.append(X)
        y_diastole_train_loss_update.append(y_diastole_train_loss)
        y_diastole_val_loss_update.append(y_diastole_val_loss)
        plots_data[name] = [X_update, y_diastole_train_loss_update, y_diastole_val_loss_update]
        diastole_train_loss = go.Scatter(
            x=X_update,
            y=y_diastole_train_loss_update,
            mode='lines+markers',
            name='train_loss'
        )
        diastole_val_loss = go.Scatter(
            x=X_update,
            y=y_diastole_val_loss_update,
            mode='lines+markers',
            name='val_loss'
        )
        data = [diastole_train_loss, diastole_val_loss]

    # Plot and embed in ipython notebook!
    # py.iplot(data, filename='line-mode')
    try:
        plot_url = py.plot(data, filename=name, overwrite=True, auto_open=False)
    except ConnectionError:
        print("Connection error")


def update_crps_graph(name, X, y_crps, y_val_crps):
    data = plots_data[name]
    X_update, y_crps_update, y_val_crps_update = data
    X_update.append(X)
    y_crps_update.append(y_crps)
    y_val_crps_update.append(y_val_crps)
    plots_data[name] = [X_update, y_crps_update, y_val_crps_update]
    crps = go.Scatter(
        x=X_update,
        y=y_crps_update,
        mode='lines+markers',
        name='crps'
    )
    val_crps = go.Scatter(
        x=X_update,
        y=y_val_crps_update,
        mode='lines+markers',
        name='val_crps'
    )
    data = [crps, val_crps]

    # Plot and embed in ipython notebook!
    # py.iplot(data, filename='line-mode')
    try:
        plot_url = py.plot(data, filename=name, overwrite=True, auto_open=False)
    except ConnectionError:
        print("Connection error")


def update_chart(name, X, y):
    data = plots_data[name]
    X_update, y_update = data
    X_update.append(X)
    y_update.append(y)
    plots_data[name] = [X_update, y_update]
    trace0 = go.Scatter(
        x=X_update,
        y=y_update,
        mode='lines+markers',
        name=name
    )
    data = [trace0]

    # Plot and embed in ipython notebook!
    # py.iplot(data, filename='line-mode')
    try:
        plot_url = py.plot(data, filename=name, overwrite=True, auto_open=False)
    except ConnectionError:
        print("Connection error")




